﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects.Visuals;

namespace DiscoveryLogic.Analytics.Modules.Nodes
{
    public class NodeShapeHelper
    {
        public static Shape CreateShape(VerticeShape shape, Size size, Color color)
        {
            double lightness = 1.55;
            switch(shape)
            {
                case VerticeShape.Circle:
                    Ellipse circle=new Ellipse();
                    circle.Fill = new RadialGradientBrush(GetColor(color,lightness), color);
                    circle.Width = size.Width;
                    circle.Height = size.Height;
                    return circle;
                case VerticeShape.Square:
                    Rectangle rect=new Rectangle();
                    rect.Width = size.Width;
                    rect.Height = size.Height;
                    rect.Fill = new RadialGradientBrush(GetColor(color, lightness), color);
                    return rect;
                case VerticeShape.Diamond:
                    Polygon diamond=new Polygon();
                    diamond.Points = new PointCollection();
                    diamond.Points.Add(new Point(0,size.Height/2));
                    diamond.Points.Add(new Point(size.Width/2, 0));
                    diamond.Points.Add(new Point(size.Width, size.Height / 2));
                    diamond.Points.Add(new Point(size.Width / 2, size.Height));
                    diamond.Width = size.Width;
                    diamond.Height = size.Height;
                    diamond.Fill = new RadialGradientBrush(GetColor(color, lightness), color);
                    return diamond;
                case VerticeShape.UpTrangle:
                    Polygon upt = new Polygon();
                    upt.Points = new PointCollection();
                    upt.Points.Add(new Point(size.Width/2, 0));
                    upt.Points.Add(new Point(0, size.Height));
                    upt.Points.Add(new Point(size.Width, size.Height));
                    upt.Width = size.Width;
                    upt.Height = size.Height;
                    upt.Fill = new RadialGradientBrush(GetColor(color, lightness), color);
                    return upt;
                case VerticeShape.DnTrangle:
                    Polygon dnt = new Polygon();
                    dnt.Points = new PointCollection();
                    dnt.Points.Add(new Point(size.Width / 2, 0));
                    dnt.Points.Add(new Point(0, size.Height));
                    dnt.Points.Add(new Point(size.Width, size.Height));
                    dnt.Width = size.Width;
                    dnt.Height = size.Height;
                    dnt.Fill = new RadialGradientBrush(GetColor(color, lightness), color);
                    return dnt;
                default:
                    return null;
            }
        }

        /// <summary>
        /// return lighter color when lightness less than 1 
        /// and darker color when lightness greater then 1
        /// </summary>
        /// <param name="color"></param>
        /// <param name="lightness"></param>
        /// <returns></returns>
        public static Color GetColor(Color color, double lightness)
        {
            int r =(int) (color.R * lightness);
            r = Math.Max(0, Math.Min(255, r));
            int g = (int)(color.G * lightness);
            g = Math.Max(0, Math.Min(255, g));
            int b = (int)(color.B * lightness);
            b = Math.Max(0, Math.Min(255, b));
            return Color.FromArgb(color.A, (byte)r, (byte)g, (byte)b);
        }
    }
}
